package com.charlotte.study.gupao.strategy.promotion.v2;

import com.charlotte.study.gupao.strategy.promotion.*;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author junjie.ding
 * @date 2020/3/16
 */
public class PromorionStrategyFactory {

    // 容器时单例模式
    private static final Map<String, IPromotionStrategy> STRATEGY_MAP = new ConcurrentHashMap<>();

    // 利用静态代码块在初始化的时候就加载
    static {
        STRATEGY_MAP.put(strategyKey.cachback, new CashBackPromotion());
        STRATEGY_MAP.put(strategyKey.coupon, new CouponPromotion());
        STRATEGY_MAP.put(strategyKey.groupbuy, new GroupbuyPromotion());
    }

    // 默认无优惠策略
    private static final IPromotionStrategy EMPTY_STRATEGY = new EmptyStrategy();


    public static IPromotionStrategy getPromotionStrategy(String promotionKey){
        if(!STRATEGY_MAP.containsKey(promotionKey)){
            // 如果没有找到优惠策略，则使用默认策略;
            return EMPTY_STRATEGY;
        }
        return STRATEGY_MAP.get(promotionKey);
    }


    private interface strategyKey{
        String coupon = "COUPON";
        String cachback = "CASHBACK";
        String groupbuy = "GROUPBUY";
    }

    public static Set<String> promotionKeys(){
        return STRATEGY_MAP.keySet();
    }
}
